package com.starblink.post.topicdetail.ui

import android.os.Bundle
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.activity.BaseTVMLoadingActivity
import com.starblink.android.basic.ext.bindProductFeeds
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.basic.route.RoutePage
import com.starblink.basic.style.title.OnTitleBarListener
import com.starblink.basic.style.title.TitleBar
import com.starblink.basic.util.FormatUtils
import com.starblink.post.databinding.ActivityHashtagDetailBinding
import com.starblink.post.topicdetail.adapter.TopicPostCardCell
import com.starblink.share.view.ShareDialog
import com.starblink.share.view.utils.ShareTypeEnum
import com.starblink.share.view.utils.ShareUrlUtils

@Route(path = RoutePage.Post.PAGE_HASHTAG)
class HashTagDetailActivity :
    BaseTVMLoadingActivity<ActivityHashtagDetailBinding, TopicDetailVM>() {

    @Autowired(name = RoutePage.Post.TOPIC_ID)
    @JvmField
    var topicId: String? = null

    override fun initViewBinding(): ActivityHashtagDetailBinding =
        ActivityHashtagDetailBinding.inflate(layoutInflater)

    private val adapter: MutableAdapter by lazyOnNone {
        MutableAdapter(viewModel.mList.value!!).apply {
            addVhDelegate {
                TopicPostCardCell(
                    it,
                    SpmPageDef.HashTagDetailRoute,
                    topicId,
                    viewModel.page,
                    viewModel.mList.value!!
                )
            }
        }
    }

    override fun onResume() {
        super.onResume()
        pageExposure(SpmPageDef.HashTagDetailRoute)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.topicId = topicId.toString()

        showStateLoading()

        viewModel.getRecommend()

        viewBinding.recyclerView.bindProductFeeds(
            adapter,
            horGap = 9, withEdge = true,
        ) as StaggeredGridLayoutManager? //单位px

        viewBinding.refresher.setEnableRefresh(false)
        viewBinding.refresher.setOnLoadMoreListener {
            viewModel.getRecommend()
            viewBinding.refresher.finishLoadMore(5000)
        }

//        viewBinding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
//            var firstVisibleItem = 0
//            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
//                super.onScrollStateChanged(recyclerView, newState)
//            }
//
//            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
//                super.onScrolled(recyclerView, dx, dy)
//                val positions =
//                    layoutManager!!.findFirstVisibleItemPositions(null)
//                if (positions != null && positions.isNotEmpty()) {
//                    firstVisibleItem = positions[0]
//                }
//                Log.d("TopicDetailActivity", "------$dy-----$dx")
//            }
//        })

        viewBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })
        viewBinding.titleBar.rightView.click {
            if (viewModel.data.value != null) {
                ShareDialog(
                    this,
                    "${ShareUrlUtils().getUrl(ShareTypeEnum.HASHTAG)}${topicId}",
                    topicName = viewModel.data.value!!.name,
                    shareCome = ShareTypeEnum.HASHTAG
                ) {}.show(supportFragmentManager, "share")
            }
        }

    }

    override fun startObserve() {
        viewModel.mList.observe(this) {
            if (viewModel.mList.value != null && viewBinding.recyclerView.adapter == null) {
                viewBinding.recyclerView.adapter = adapter
            }
            if (viewModel.mList.value != null) {
                adapter.notifyDataSetChanged()
            }
            if (viewBinding.refresher.isLoading) {
                viewBinding.refresher.finishLoadMore()
            }
        }
        viewModel.data.observe(this) {
            showStateSuccess()
            if (viewModel.data.value != null) {
                viewBinding.titleBar.titleView.text = viewModel.data.value!!.name
                viewBinding.tvPost.text = viewModel.data.value!!.relativePostCnt?.let { it1 ->
                    "${FormatUtils.formatRelatedPostNum(it1)} posts"
                }
            }
        }
    }

}